package com.canyou.generator;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.canyou.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
 *
 * @author cgz
 */
public class MySqlCodeGenerator {

    public static void main(String[] args) {
        //generator("system", "sys_.*","sys_");
        //generator("basic", "basic_sms_send", "basic_");
        //generator("basic", "basic_media", "basic_");
        //generator("activiti", "ACT_RE_DEPLOYMENT", "ACT_RE_");
        //generator("activiti", "act_form_data", "act_");
        //generator("activiti", "act_form_data_item", "act_");
        //generator("activiti", "act_form_data_val", "act_");
        generator("archives", "equ_archives", "equ_");
        generator("archives", "equ_archives_log", "equ_");
        generator("archives", "equ_category", "equ_");
    }

    /**
     * 代码生成器
     *
     * @param moduleName
     * @param tableName
     */
    public static void generator(String moduleName, String tableName, String tablePrefix) {
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/cys-service/src/main/java");
        gc.setAuthor("canyou");
        gc.setOpen(false);
        gc.setSwagger2(true);
        ///gc.setDateType(DateType.TIME_PACK);
        gc.setServiceName("%sService");
        ///gc.setFileOverride(true);
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://rm-bp11sued1db83gzlixo.mysql.rds.aliyuncs.com:3306/canyou" +
                "?useUnicode=true&useSSL=false&characterEncoding=utf8");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("wgrroot");
        dsc.setPassword("A-a13146414");
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.canyou");
        pc.setService(moduleName + ".service");
        pc.setServiceImpl(moduleName + ".service.impl");
        pc.setMapper(moduleName + ".mapper");
        pc.setEntity(moduleName + ".model");
        pc.setController(moduleName);
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
            }
        };

        // 自定义配置会被优先输出
        List<FileOutConfig> focList = new ArrayList<>();
        focList.add(getMapperFileOutConfig(projectPath, moduleName));
        focList.add(getControllerFileOutConfig(projectPath, moduleName));
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setController(null);
        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setSuperControllerClass("com.canyou.api.BaseApiController");
        strategy.setSuperEntityClass("com.canyou.base.BaseModel");
        strategy.setEntityLombokModel(true);
        strategy.setInclude(tableName);
        strategy.setSuperEntityColumns("id", "create_by", "modify_by",
                "create_time", "modify_time", "version", "is_deleted");
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setRestControllerStyle(true);
        //乐观锁字段名称
        strategy.setVersionFieldName("version");
        //逻辑删除字段名称
        strategy.setLogicDeleteFieldName("is_deleted");
        //添加字段注释
        strategy.setEntityTableFieldAnnotationEnable(true);

        if (StringUtils.isNotEmpty(tablePrefix)) {
            strategy.setTablePrefix(tablePrefix);
        }
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }

    /**
     * Controller文件输出配置
     *
     * @param projectPath
     * @param moduleName
     * @return
     */
    private static FileOutConfig getControllerFileOutConfig(String projectPath, String moduleName) {
        String controllerTplPath = "/templates/controller.java.ftl";
        return new FileOutConfig(controllerTplPath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名
                return projectPath + "/cys-webapi/src/main/java/com/canyou/"
                        + moduleName + "/" + tableInfo.getEntityName()
                        + "Controller" + StringPool.DOT_JAVA;
            }
        };
    }

    /**
     * Mapper文件输出配置
     *
     * @param projectPath
     * @param moduleName
     * @return
     */
    private static FileOutConfig getMapperFileOutConfig(String projectPath, String moduleName) {
        String mapperTplPath = "/templates/mapper.xml.ftl";
        return new FileOutConfig(mapperTplPath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名
                return projectPath + "/cys-service/src/main/resources/mapper/" + moduleName
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        };
    }
}